package com.hllxd.hism.star.basic.service.service;

import com.alibaba.fastjson.JSON;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.PermissionInfoMapper;
import com.hllxd.hism.star.basic.service.model.PermissionInfo;
import com.hllxd.hism.star.basic.service.model.RoleInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 * v1/user/userInfo.do
 */
@Slf4j
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionInfoMapper permissionInfoMapper;

    @Autowired
    private RoleService roleService;

//    @Autowired
//    private RedisTemplate<String, String> redisTemplate;

    public static final String KEY_PREFIX = "star_permission_";
    public static final String PERMISSION_CACHE = KEY_PREFIX + "permission_cache";


    @Override
    public List<PermissionInfo> listPermission() {
        return permissionInfoMapper.getPermissionInfo();
    }

    @Override
    public List<RoleInfo> listPermissionRole(String functionLabel) {

        return roleService.listRoleByLabel(functionLabel);
    }


    @Override
    public Long delPermissionRole(Long id) {
        return roleService.delPermissionRole(id);
    }

    @Override
    public Object setPermission(PermissionInfo aa) {
        log.info("set param: {}", JSON.toJSONString(aa));
        if (aa.id == null || aa.id == 0) {
            long mysqlId = permissionInfoMapper.insertPermissionInfo(aa);
            log.info("权限信息写入缓存");
//            redisTemplate.opsForHash().put(PERMISSION_CACHE, aa.getFunctionItem(), aa.getId() + "");
            return mysqlId;
        } else {
            PermissionInfo origin = permissionInfoMapper.getOnePermissionInfo(aa.getId());
            if (!origin.getFunctionItem().equals(aa.getFunctionItem())) {
                log.info("权限item已经更改，先移除后增加");
//                redisTemplate.opsForHash().delete(PERMISSION_CACHE, origin.getFunctionItem());
//                redisTemplate.opsForHash().put(PERMISSION_CACHE, aa.getFunctionItem(), aa.getId().toString());
            }
            return permissionInfoMapper.updatePermissionInfo(aa);
        }
    }

    @Override
    public PermissionInfo getPermissionInfo(Long id) {
        return permissionInfoMapper.getOnePermissionInfo(id);
    }

    @Override
    public Object delPermissionInfo(Long id) {
        PermissionInfo aa = permissionInfoMapper.getOnePermissionInfo(id);
//        redisTemplate.opsForHash().delete(PERMISSION_CACHE, aa.getFunctionItem());

        List<RoleInfo> roleInfoList = permissionInfoMapper.getRoleInfoByPermissionId(aa.getId());
        for (RoleInfo roleInfo : roleInfoList) {
            String ids = roleInfo.getPermissionIds();
            String[] split = ids.split(",");
            StringBuilder sb = new StringBuilder();
            for (String permissionId : split) {
                if (permissionId.equals(aa.getId().toString())) {
                    continue;
                }
                sb.append(permissionId).append(",");
            }
            if (sb.length() >= 2) {
                sb.deleteCharAt(sb.length() - 1);
            }
            roleInfo.setPermissionIds(sb.toString());
            roleService.setRoleInfo(roleInfo);
        }
        return permissionInfoMapper.updatePermissionAction(id);
    }
}
